package com.cotton.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import com.cotton.service.IBaseManager;
import com.cotton.service.IRootManager;
import com.uusoft.cache.redix.MessHashPacker;
import com.uusoft.cache.redix.MessListPacker;

/**
 * 版权：上海投投金融信息服务有限公司
 * 功能：实现类公用模块
 * 作者：朱伟峰
 * 邮箱：zhuweifeng24@gmail.com
 * 日期：2016年9月26日
 */
public class BaseManager implements IBaseManager, IRootManager
{
	private static final Logger logger = LoggerFactory.getLogger(BaseManager.class);
	
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;
	private MessListPacker rlp;
	private MessHashPacker hp;
	
//	@Resource
//	private LotteryMapper lotteryMapper;
//	@Resource
//	private LotteryStaticMapper lotteryStaticMapper;
	
	@PostConstruct
	@Override
	public void initialize()
	{
		rlp = new MessListPacker(redisTemplate);
		hp = new MessHashPacker(redisTemplate);
		this.destroy();
		
		// TODO 样例
//		logger.info(" ---------- 初始化 --> 开奖数据源 --> 缓存：start ---------- ");
//		LotteryExample lotteryExample = new LotteryExample();
//		lotteryExample.createCriteria().andStatusEqualTo(0);
//		List<Lottery> _lotteryList = lotteryMapper.selectByExample(lotteryExample);
//		if (_lotteryList != null && !_lotteryList.isEmpty())
//		{
//			rlp.lpush(RedisConstant._LOTTERY_LIST_KEY, _lotteryList);
//			for (Lottery lottery : _lotteryList)
//				hp.hset(RedisConstant._LOTTERY_MAP_KEY, lottery.getNumberNo().toString(), lottery);
//		}
//		logger.info(" ---------- 初始化 --> 开奖数据源 --> 缓存：over ---------- ");
		
	}

	@PreDestroy
	@Override
	public void destroy()
	{
		// TODO 样例
//		rlp.del(RedisConstant._LOTTERY_LIST_KEY);
//		hp.del(RedisConstant._LOTTERY_MAP_KEY);
	}

	@Override
	public void onAdd(Object entity)
	{
		if (entity == null)
			return;
		
		// TODO 样例
//		if (entity instanceof Lottery)
//		{
//			Lottery bean = (Lottery)entity;
//			Long numberNo = bean.getNumberNo().longValue();
//			rlp.lpush(RedisConstant._LOTTERY_LIST_KEY, bean);
//			hp.hset(RedisConstant._LOTTERY_MAP_KEY, numberNo.toString(), bean);
//		}
	}

	@Override
	public void onEdit(Object entity)
	{
		if (entity == null)
			return;
		
		this.onDelete(entity);	// 先删除缓存
		this.onAdd(entity);		// 再添加缓存
	}

	@Override
	public void onDelete(Object entity)
	{
		if (entity == null)
			return;
		
		// TODO 样例
//		if (entity instanceof Lottery)
//		{
//			Lottery bean = (Lottery)entity;
//			Long numberNo = bean.getNumberNo().longValue();
//			rlp.lrem(RedisConstant._LOTTERY_LIST_KEY, 0, this.getLottery(numberNo));
//			hp.hdel(RedisConstant._LOTTERY_MAP_KEY, numberNo.toString());
//		}
	}

	@Override
	public void rollback(Object entity, Object primaryId)
	{
		if (entity == null)
			return;
		if (primaryId == null)
			return;
		
		try
		{
			// TODO 样例
//			if (entity instanceof Lottery)
//			{
//				Lottery bean = lotteryMapper.selectByPrimaryKey((Long)primaryId);
//				if (bean != null) 
//					hp.hset(RedisConstant._LOTTERY_MAP_KEY, bean.getNumberNo().toString(), bean);
//			}
		}
		catch (Exception e)
		{
			logger.error("", e);
		}
	}

	@Override
	public <T extends Serializable> void setCache(String key, String field,
			T val)
	{
		hp.hset(key, field, val);
	}

	@Override
	public <T extends Serializable> T getCache(String key, String field,
			Class<T> cls)
	{
		return hp.hget(key, field, cls);
	}

	@Override
	public <T extends Serializable> List<T> getListCache(String key, Class<T> cls)
	{
		return rlp.lrange(key, cls, 0, -1);
	}
	
	@Override
	public <T extends Serializable> void setListCache(String key, T t)
	{
		rlp.lpush(key, t);
	}

	@Override
	public void removeListCache(String key)
	{
		rlp.del(key);
	}

	@Override
	public <T extends Serializable> void setListHashCache(String key, Class<T> cls)
	{
		rlp.lpush(key, cls);
	}

	@Override
	public <T extends Serializable> void removeListHashCache(String key, T t)
	{
		rlp.lrem(key, 0, t);
	}

	@Override
	public void removeAllCache(String key)
	{
		hp.del(key);
	}

}
